<?php
namespace Ucmaim\Model;
use Think\Model;


class ContentModel extends Model{

    public function getList($condition = []){
        $this->alias('A');
        if( !empty($condition)){
            $this->where($condition);
        }
        $this->field('A.*, B.name AS category_name');
        $this->join('uc_content_category B on A.category = B.id');
        return $this->order('update_time desc')->select();
    }

    public function contentInfo($id){
        if(! is_numeric($id)) return [];
        $map['A.id'] = $id;
        $this->field('A.*, B.name AS category_name');
        $this->alias('A')->join('uc_content_category B on A.category = B.id');
        return $this->where($map)->find();
    }

    // 添加内容
    public function addContent($data){
        if($this->create($data)){
            $this->add();
            return true;
        }
        return false;
    }

    // 修改内容
    public function updateContent($id, $data){
        if(! is_numeric($id)) return false;
        $map['id'] = $id;
        return $this->where($map)->save($data) ? true : false;
    }

    // 删除一条内容
    public function deleteContent($id){
        if(! is_numeric($id)) return false;
        $map['id'] = $id;
        return $this->where($map)->delete() ? true : false;
    }

}